/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import ObjetData.Accessoire;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.Statement;

/**
 *
 * @author M-Soft
 */
public class DaoAccessoire extends DAO<Accessoire> {

    private HashMap<Integer, Accessoire> accessoires = new HashMap<Integer, Accessoire>();
    private Integer lastIdArticle;
    private Integer lastIdAccessoire;
    private SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public HashMap<Integer, Accessoire> getAll() {
        try {
            ResultSet result = Connect.getConnexion().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT ID_ACCESSOIRES, LABEL, POIDS, QUANTITE, PRIXHT, TVA  FROM APP.accessoires inner join APP.article using(id_article)");
            while (result.next()) {
                Accessoire access1 = new Accessoire(result.getInt(1), result.getString(2), result.getDouble(3));
                access1.setPrixht(result.getInt("PRIXHT"));
                access1.setQuantite(result.getInt("QUANTITE"));
                access1.setTva(result.getInt("TVA"));
                accessoires.put(result.getInt(1), access1);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DaoAnimal.class.getName()).log(Level.SEVERE, null, ex);
        }
        return this.accessoires;
    }

    @Override
    public Accessoire find(int id) {
        if (id > 0) {
            Accessoire access1 = new Accessoire();
            ResultSet result = null;
            try {
                result = Connect.getConnexion().createStatement(
                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_READ_ONLY).executeQuery(
                        "SELECT ID_ACCESSOIRES, LABEL, POIDS, ID_ESPECE, DATE_NAISSANCE, PHOTO, PRIXHT, DESCRIPTION FROM APP.animal inner join APP.article using(id_article) where id_animal = " + id);
                if (result.first()) {
                    result.first();
                    access1.setIdAccessoires(result.getInt("ID_ACCESSOIRES"));
                    access1.setLabel(result.getString("LABEL"));
                    access1.setPoids(result.getDouble("POIDS"));
                    //ani1.setIdEspece(result.getInt("ID_ESPECE"));
                    //ani1.setDateNaissance(result.getDate("DATE_NAISSANCE"));
                    //ani1.setPrixht(result.getInt("PRIXHT"));
                    //ani1.setDescription(result.getString("DESCRIPTION"));
                    //ani1.setPhoto(result.getString("PHOTO"));
                }

                //return (Animal) result.first();
            } catch (SQLException ex) {
                Logger.getLogger(DaoAnimal.class.getName()).log(Level.SEVERE, null, ex);
            }
            return access1;
        } else {
            throw new DaoException("Animal d'id [" + id + "] inconnue", 2);
        }
    }

    @Override
    public boolean delete(Accessoire obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean save(Accessoire obj) {
        /*
        System.out.println("PASSAGE DANS : save(Animal obj DaoAnimal)");
        System.out.println( obj.getLabel() +" <> "+ obj.getLabel().getClass() );
        System.out.println( obj.getQuantite() +" <> "+ obj.getQuantite() );
        System.out.println( obj.getPrixht() +" <> "+ obj.getPrixht() );
        System.out.println( obj.getTva() +" <> "+ obj.getTva() );
        System.out.println( obj.getDescription() +" <> "+ obj.getDescription().getClass() );
        System.out.println( obj.getReference() +" <> "+ obj.getReference().getClass() );
        System.out.println( obj.getPhoto() +" <> "+ obj.getPhoto().getClass() );
        System.out.println( obj.getIdCategorie() +" <> "+ obj.getIdCategorie() );
        System.out.println( obj.getIdEspece() +" <> "+ obj.getIdEspece() );
         */

        try {
            Statement stmt = Connect.getConnexion().createStatement();
            //Connect.getConnexion().setAutoCommit(false);
            Integer id = this.getLastIdArticle() + 1;
            //String insert="INSERT INTO APP.ARTICLE VALUES ("+this.getLastIdArticle()+1+",'"+obj.getLabel()+"','"+obj.getQuantite()+"','"+obj.getPrixht()+"','"+obj.getTva()+"','"+obj.getDescription()+"','"+obj.getReference()+"','"+obj.getPhoto()+"',"+obj.getIdCategorie()+",'"+obj.getIdEspece()+"')";
            String insert = "INSERT INTO APP.ARTICLE VALUES (" + id + ",'" + obj.getLabel() + "'," + obj.getQuantite() + "," + obj.getPrixht() + "," + obj.getTva() + ",'" + obj.getDescription() + "','" + obj.getReference() + "','" + obj.getPhoto() + "'," + obj.getIdCategorie() + "," + obj.getIdEspece() + ")";
            stmt.executeUpdate(insert);
            //Connect.getConnexion().prepareStatement(insert);
            //stmt.addBatch(insert);
            id = this.getLastIdAccessoire() + 1;
            insert = "INSERT INTO APP.ACCESSOIRES VALUES (" + id + ",'" + obj.getPoids() + "'," + this.getLastIdArticle() + ")";
            stmt.executeUpdate(insert);
            //stmt.addBatch(insert);
            //stmt.executeBatch();
            stmt.close();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DaoUser.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    @Override
    public HashMap<Integer, Accessoire> search(String searchValue, Integer idCatValue) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private Integer getLastIdArticle() {
        ResultSet result = null;
        try {
            result = Connect.getConnexion().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT MAX(ID_ARTICLE) as ID_ARTICLE "
                    + "FROM APP.ARTICLE  ");
            if (result.first()) {
                result.first();
                this.lastIdArticle = result.getInt("ID_ARTICLE");
            }
        } catch (SQLException ex) {
            Logger.getLogger(DaoUser.class.getName()).log(Level.SEVERE, null, ex);
            this.lastIdArticle = 0;
        }
        return this.lastIdArticle;
    }

    private Integer getLastIdAccessoire() {
        ResultSet result = null;
        try {
            result = Connect.getConnexion().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY).executeQuery(
                    "SELECT MAX(ID_ACCESSOIRES) as ID_ACCESSOIRES "
                    + "FROM APP.ACCESSOIRES  ");
            if (result.first()) {
                result.first();
                this.lastIdAccessoire = result.getInt("ID_ACCESSOIRES");
            }
        } catch (SQLException ex) {
            Logger.getLogger(DaoUser.class.getName()).log(Level.SEVERE, null, ex);
            this.lastIdAccessoire = 0;
        }
        return this.lastIdAccessoire;
    }
}
